package andris.android.mobilarena.widget;

import andris.android.mobilarena.MobilarenaApplication;
import andris.android.mobilarena.R;
import andris.android.mobilarena.provider.NewsListAdapter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class NewsFragment extends Fragment {

	private LinearLayout mLayout;
	private ListView mList;
	private RadioGroup mRadioGroup;
	private NewsListAdapter mAdapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mLayout = (LinearLayout) inflater.inflate(R.layout.news_fragment, null);
		init();
		return mLayout;
	}

	private void init() {
		mList = (ListView) mLayout.findViewById(R.id.news_listview);
		mAdapter = new NewsListAdapter(getActivity(), MobilarenaApplication.news.getNewsList());
		mList.setAdapter(mAdapter);

		mRadioGroup = (RadioGroup) mLayout.findViewById(R.id.news_radiogroup);
		mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (checkedId == R.id.news_radio_1)
					mAdapter.changeSelection(0);
				else if (checkedId == R.id.news_radio_2)
					mAdapter.changeSelection(1);
				else
					mAdapter.changeSelection(2);

				mList.invalidate();
			}
		});
	}

}
